package com.example.it.studyviewflipper;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.ViewFlipper;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class HomeActivity extends AppCompatActivity implements View.OnLayoutChangeListener {

    private RadioButton rb1, rb2, rb3;

    private ViewFlipper viewFlipper;
    private List<RadioButton> radioButtonList;
    private List<ImageView> imageViewList;
    private List<Integer> imageIdsList;


    private void initFindView() {
        viewFlipper = (ViewFlipper) this.findViewById(R.id.fl_viewflipper);

        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        rb3 = (RadioButton) findViewById(R.id.rb3);
    }

    private void initImage() {
        imageIdsList = new ArrayList<Integer>();
        imageIdsList.add(R.drawable.grape);
        imageIdsList.add(R.drawable.orange_yellow);
        imageIdsList.add(R.drawable.grapegreen);
    }

    private void initDot() {
        radioButtonList = new ArrayList<RadioButton>();

        radioButtonList.add(rb1);
        radioButtonList.add(rb2);
        radioButtonList.add(rb3);
    }

    private void initImageView() {

        imageViewList = new ArrayList<ImageView>();
        for (int i = 0; i < 3; i++) {
            ImageView imageView = new ImageView(this);

            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(-1, -1);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            Glide.with(this).load(this.imageIdsList.get(i)).into(imageView);

            imageView.setId(new Random().nextInt(100000) );

            imageViewList.add(imageView);
        }
    }

    private void initVieFlipper() {
        for (int i = 0; i < imageViewList.size(); i++) {
            this.viewFlipper.addView(imageViewList.get(i));
        }

        this.viewFlipper.addOnLayoutChangeListener(this);
    }

    private int idAreEquals(int id) {
        int value = -1;

        for (int i = 0; i < imageViewList.size(); i++) {
            ImageView iv = imageViewList.get(i);
            if (iv.getId() == id) {
                value = i;
                break;
            }
        }
        return value;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        initFindView();
        initDot();
        initImage();
        initImageView();

        initVieFlipper();
    }


    /*
    * 当ViewFilter布局改变的时候触发的事件
    * */
    @Override
    public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
        //获取到当前的ViewFlipper中显示的视图，获取其Id，用来确定使得那个RadioButton应该被选中
        int id = this.viewFlipper.getCurrentView().getId();

        int value = idAreEquals(id);
        if (value != -1) {
            radioButtonList.get(value).setChecked(true);
        }
    }
}
